import React from 'react';
import {hasPermission} from "@/utils/utils";
import {Empty} from "antd";

interface Props {
  code: string;
  noPermissionMessage?: string;
  children?: React.ReactNode;
}

/**
 * 根据hasPermission 和code 来判断children是否显示
 * 一般用于前端权限控制是否显示某个按钮等
 */
const Permission: React.FC<Props> = props => {
  let {code, noPermissionMessage = '您没有权限,请联系管理员', children} = props;
  const has = hasPermission(code);
  if (has) {
    return <>{children}</>;
  }
  if (noPermissionMessage) {
    return (<Empty description={noPermissionMessage}/>);
  }
  return null;
};
export default React.memo(Permission);
